package com.seavenois.alife2;

import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/*************************************************/
/* The Dialog that it's shown when a box is ******/
/* tapped. It uses only one xml layout, but ******/
/* displayed options depends on the box tapped. **/
/*************************************************/
public class DialogInfo extends DialogFragment {
	//Scenario
	byte scenario;
	//Strings to write on the dialog
	String title, description, upgradeInfo;
	//TextViews to show the info in the dialog
	TextView textTitle;
	TextView textDescription;
	TextView textUpgradeInfo;
	TextView textMaroon, textPink, textYellow, textBlue, textRed, textGreen, textPurple, textDoor, textAcademy, textTopia, textStation, textSiphon;
	//ImageView for the icon
	ImageView icon;
	int iconId;
	//For the upgrade Icon
	ImageView upgradeIcon;
	int upgradeIconId;
	//For the parameter bar
	int barValue, barMaxValue;
	String barTitle, barText;
	//The main layout, references in case it has to be pragmatically resized
	LinearLayout layout;
	//Layouts containing different sets of options, to be hidden depending on the box type 
	LinearLayout layoutBuild, layoutUpgrade, layoutParameter, layoutSiphon;
	//Buttons with common options, some will be hidden depending on the box type
	Button buttonDestroy, buttonClose, buttonUpgrade;
	//ImageButtons array to build the areas, can be hidden depending on the box type
	ImageButton[] buttonArea = new ImageButton[7];
	//ImageButtons array to build buildings, can be hidden depending on the box type
	ImageButton[] buttonBuilding = new ImageButton[5];
	//Box characteristics. Depending on this, some options will or will not be displayed
	boolean buildable, parametrizable, destroyable, upgradeable, river;
	int width, box, nextBuilding;
	
	//Must be called before showing the dialog. It sets the variables that determine which parts of the dialog are to show or to hide
	//Arguments are in arrays for simplicity. Check code to see what is what
	public void setData(byte _scen, boolean[] booleans, int[] integers, String[] strings){
		scenario = _scen;
		width = (int) (integers[1] * 0.9);
		box = integers[0];
		buildable = booleans[0];
		river = booleans[1];
		upgradeable = booleans[2];
		destroyable = booleans[3];
		parametrizable = booleans[4];
		title = strings[0];
		description = strings[1];
		iconId = integers[4];
		upgradeInfo = strings[2];
		upgradeIconId = integers[5];
		barTitle = strings[3];
		barText = strings[4];
		barValue = integers[2];
		barMaxValue = integers[3];
		nextBuilding = integers[6];
	}
	
	//Called each time the dialog is to be shown
	//Configures the view that will be contained in the dialog window
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		//Set layout for the dialog
		View view = inflater.inflate(R.layout.dialog_info, container, false);
		//Set text elements and apply strings
		textTitle = (TextView) view.findViewById(R.id.dialogInfoTitle);
		textTitle.setText(title);
		textDescription = (TextView) view.findViewById(R.id.dialogInfoDescription);
		textDescription.setText(description);
		//Set icon image
		icon = (ImageView) view.findViewById(R.id.dialogInfoIcon);
		icon.setImageResource(iconId);
		//Assign main layout and set minimum dimension (depending on the screen dimensions)
		layout = (LinearLayout) view.findViewById(R.id.dialogInfoLayout);
		layout.setMinimumWidth(width);
		layout.setMinimumHeight(width / 2);
		//Assign buttons and set OnClickListeners
		buttonDestroy = (Button) view.findViewById(R.id.dialogInfoDestroy);
		buttonUpgrade = (Button) view.findViewById(R.id.dialogUpgradeButton);
		buttonClose = (Button) view.findViewById(R.id.dialogInfoClose);
		buttonClose.setOnClickListener(btnCloseOnClickListener);
		buttonArea[0] = (ImageButton) view.findViewById(R.id.dialogBuildMaroon);
		buttonArea[1] = (ImageButton) view.findViewById(R.id.dialogBuildPink);
		buttonArea[2] = (ImageButton) view.findViewById(R.id.dialogBuildYellow);
		buttonArea[3] = (ImageButton) view.findViewById(R.id.dialogBuildBlue);
		buttonArea[4] = (ImageButton) view.findViewById(R.id.dialogBuildRed);
		buttonArea[5] = (ImageButton) view.findViewById(R.id.dialogBuildGreen);
		buttonArea[6] = (ImageButton) view.findViewById(R.id.dialogBuildPurple);
		buttonArea[0].setOnClickListener(btnMaroonOnClickListener);
		buttonArea[1].setOnClickListener(btnPinkOnClickListener);
		buttonArea[2].setOnClickListener(btnYellowOnClickListener);
		buttonArea[3].setOnClickListener(btnBlueOnClickListener);
		buttonArea[4].setOnClickListener(btnRedOnClickListener);
		buttonArea[5].setOnClickListener(btnGreenOnClickListener);
		buttonArea[6].setOnClickListener(btnPurpleOnClickListener);
		buttonBuilding[0] = (ImageButton) view.findViewById(R.id.dialogBuildDoor);
		buttonBuilding[1] = (ImageButton) view.findViewById(R.id.dialogBuildAcademy);
		buttonBuilding[2] = (ImageButton) view.findViewById(R.id.dialogBuildTopia);
		buttonBuilding[3] = (ImageButton) view.findViewById(R.id.dialogBuildStation);
		buttonBuilding[4] = (ImageButton) view.findViewById(R.id.dialogBuildSiphon);
		buttonBuilding[0].setOnClickListener(btnDoorOnClickListener);
		buttonBuilding[1].setOnClickListener(btnAcademyOnClickListener);
		buttonBuilding[2].setOnClickListener(btnTopiaOnClickListener);
		buttonBuilding[3].setOnClickListener(btnStationOnClickListener);
		buttonBuilding[4].setOnClickListener(btnSiphonOnClickListener);
		buttonDestroy.setOnClickListener(btnDestroyOnClickListener);
		buttonUpgrade.setOnClickListener(btnUpgradeOnClickListener);
		//Hide elements depending on the dialog data
		if (destroyable == false)
			buttonDestroy.setVisibility(View.GONE);
		layoutBuild = (LinearLayout) view.findViewById(R.id.dialogInfoLayoutBuild);
		layoutSiphon = (LinearLayout) view.findViewById(R.id.dialogInfoLayoutSiphon);
		if (buildable == false){
			layoutBuild.setVisibility(View.GONE);
			layoutSiphon.setVisibility(View.GONE);
		}
		else{
			if (river == true)
				layoutBuild.setVisibility(View.GONE);
			else
				layoutSiphon.setVisibility(View.GONE);
		}
		layoutParameter = (LinearLayout) view.findViewById(R.id.dialogInfoLayoutParameter);
		if (parametrizable == false)
			layoutParameter.setVisibility(View.GONE);
		layoutUpgrade = (LinearLayout) view.findViewById(R.id.dialogInfoLayoutUpgrade);
		if (upgradeable == false)
			layoutUpgrade.setVisibility(View.GONE);
		//Upgrade menu
		upgradeIcon = (ImageView) view.findViewById(R.id.dialogUpgradeIcon);
		upgradeIcon.setImageResource(upgradeIconId);
		textUpgradeInfo = (TextView) view.findViewById(R.id.dialogUpgradeText);
		textUpgradeInfo.setText(upgradeInfo);
		//Labels for the building names
    	textMaroon = (TextView) view.findViewById(R.id.dialogTextMaroon);
    	textPink = (TextView) view.findViewById(R.id.dialogTextPink);
    	textYellow = (TextView) view.findViewById(R.id.dialogTextYellow);
    	textBlue = (TextView) view.findViewById(R.id.dialogTextBlue);
    	textRed = (TextView) view.findViewById(R.id.dialogTextRed);
    	textGreen = (TextView) view.findViewById(R.id.dialogTextGreen);
    	textPurple = (TextView) view.findViewById(R.id.dialogTextPurple);
    	textDoor = (TextView) view.findViewById(R.id.dialogTextDoor);
    	textAcademy = (TextView) view.findViewById(R.id.dialogTextAcademy);
    	textTopia = (TextView) view.findViewById(R.id.dialogTextTopia);
    	textStation = (TextView) view.findViewById(R.id.dialogTextStation);
    	textSiphon = (TextView) view.findViewById(R.id.dialogTextSiphon);
    	switch (scenario){
    		case Values.SCENARIO_HEAVEN:
    			textMaroon.setText(getString(R.string.chastity) + "\n(" + Integer.toString(Values.HOSTING_1_PRICE) + "ł)");
		    	textPink.setText(getString(R.string.temperance) + "\n(" + Integer.toString(Values.HOSTING_1_PRICE) + "ł)");
		    	textYellow.setText(getString(R.string.charity) + "\n(" + Integer.toString(Values.HOSTING_1_PRICE) + "ł)");
		    	textBlue.setText(getString(R.string.diligence) + "\n(" + Integer.toString(Values.HOSTING_1_PRICE) + "ł)");
		    	textRed.setText(getString(R.string.patience) + "\n(" + Integer.toString(Values.HOSTING_1_PRICE) + "ł)");
		    	textGreen.setText(getString(R.string.kindness) + "\n(" + Integer.toString(Values.HOSTING_1_PRICE) + "ł)");
		    	textPurple.setText(getString(R.string.humillity) + "\n(" + Integer.toString(Values.HOSTING_1_PRICE) + "ł)");
		    	textDoor.setText(getString(R.string.heavenDoor) + "\n(" + Integer.toString(Values.DOOR_1_PRICE) + "ł)");
		    	textAcademy.setText(getString(R.string.heavenAcademy) + "\n(" + Integer.toString(Values.ACADEMY_1_PRICE) + "ł)");
		    	textTopia.setText(getString(R.string.heavenTopia) + "\n(" + Integer.toString(Values.TOPIA_1_PRICE) + "ł)");
		    	textStation.setText(getString(R.string.station) + "\n(" + Integer.toString(Values.STATION_1_PRICE) + "ł)");
		    	textSiphon.setText(getString(R.string.siphon) + "\n(" + Integer.toString(Values.SIPHON_1_PRICE) + "ł)");
		    	break;
    		case Values.SCENARIO_HELL:
		    	textMaroon.setText(getString(R.string.lust) + "\n(" + Integer.toString(Values.HOSTING_1_PRICE) + "ł)");
		    	textPink.setText(getString(R.string.gluttony) + "\n(" + Integer.toString(Values.HOSTING_1_PRICE) + "ł)");
		    	textYellow.setText(getString(R.string.greed) + "\n(" + Integer.toString(Values.HOSTING_1_PRICE) + "ł)");
		    	textBlue.setText(getString(R.string.laziness) + "\n(" + Integer.toString(Values.HOSTING_1_PRICE) + "ł)");
		    	textRed.setText(getString(R.string.anger) + "\n(" + Integer.toString(Values.HOSTING_1_PRICE) + "ł)");
		    	textGreen.setText(getString(R.string.envy) + "\n(" + Integer.toString(Values.HOSTING_1_PRICE) + "ł)");
		    	textPurple.setText(getString(R.string.vanity) + "\n(" + Integer.toString(Values.HOSTING_1_PRICE) + "ł)");
		    	textDoor.setText(getString(R.string.hellDoor) + "\n(" + Integer.toString(Values.DOOR_1_PRICE) + "ł)");
		    	textAcademy.setText(getString(R.string.hellAcademy) + "\n(" + Integer.toString(Values.ACADEMY_1_PRICE) + "ł)");
		    	textTopia.setText(getString(R.string.hellTopia) + "\n(" + Integer.toString(Values.TOPIA_1_PRICE) + "ł)");
		    	textStation.setText(getString(R.string.station) + "\n(" + Integer.toString(Values.STATION_1_PRICE) + "ł)");
		    	textSiphon.setText(getString(R.string.siphon) + "\n(" + Integer.toString(Values.SIPHON_1_PRICE) + "ł)");
		    	break;
    	}
    	//Return the view that will be shown in the dialog
    	return view;
    }
    
	//Called each time the dialog is created
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Dialog dialog = super.onCreateDialog(savedInstanceState);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		return dialog;
	}
	
	//Buttons listeners
	//Dismiss button
	private Button.OnClickListener btnCloseOnClickListener = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0) {
			dismiss();
		}
	};
	
	//Area builder buttons
	private Button.OnClickListener btnMaroonOnClickListener = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0) {
			Game callingActivity = (Game) getActivity();
			callingActivity.build(box, Values.BUILDING_MAROON_1);
			dismiss();
		}
	};
	
	private Button.OnClickListener btnPinkOnClickListener = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0) {
			Game callingActivity = (Game) getActivity();
			callingActivity.build(box, Values.BUILDING_PINK_1);
			dismiss();
		}
	};
	
	private Button.OnClickListener btnYellowOnClickListener = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0) {
			Game callingActivity = (Game) getActivity();
			callingActivity.build(box, Values.BUILDING_YELLOW_1);
			dismiss();
		}
	};
	
	private Button.OnClickListener btnBlueOnClickListener = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0) {
			Game callingActivity = (Game) getActivity();
			callingActivity.build(box, Values.BUILDING_BLUE_1);
			dismiss();
		}
	};
	
	private Button.OnClickListener btnRedOnClickListener = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0) {
			Game callingActivity = (Game) getActivity();
			callingActivity.build(box, Values.BUILDING_RED_1);
			dismiss();
		}
	};
	
	private Button.OnClickListener btnGreenOnClickListener = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0) {
			Game callingActivity = (Game) getActivity();
			callingActivity.build(box, Values.BUILDING_GREEN_1);
			dismiss();
		}
	};
	
	private Button.OnClickListener btnPurpleOnClickListener = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0) {
			Game callingActivity = (Game) getActivity();
			callingActivity.build(box, Values.BUILDING_PURPLE_1);
			dismiss();
		}
	};
	
	//Building buttons
	private Button.OnClickListener btnDoorOnClickListener = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0) {
			Game callingActivity = (Game) getActivity();
			callingActivity.build(box, Values.BUILDING_DOOR_1);
			dismiss();
		}
	};
	
	private Button.OnClickListener btnAcademyOnClickListener = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0) {
			Game callingActivity = (Game) getActivity();
			callingActivity.build(box, Values.BUILDING_ACADEMY_1);
			dismiss();
		}
	};
	
	private Button.OnClickListener btnTopiaOnClickListener = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0) {
			Game callingActivity = (Game) getActivity();
			callingActivity.build(box, Values.BUILDING_TOPIA_1);
			dismiss();
		}
	};
	
	private Button.OnClickListener btnStationOnClickListener = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0) {
			Game callingActivity = (Game) getActivity();
			callingActivity.build(box, Values.BUILDING_STATION_1);
			dismiss();
		}
	};
	
	private Button.OnClickListener btnSiphonOnClickListener = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0) {
			Game callingActivity = (Game) getActivity();
			callingActivity.build(box, Values.BUILDING_SIPHON_1);
			dismiss();
		}
	};
	
	private Button.OnClickListener btnDestroyOnClickListener = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0) {
			Game callingActivity = (Game) getActivity();
			callingActivity.destroy(box);
			dismiss();
		}
	};
	
	private Button.OnClickListener btnUpgradeOnClickListener = new Button.OnClickListener(){
		@Override
		public void onClick(View arg0) {
			Game callingActivity = (Game) getActivity();
			callingActivity.build(box, nextBuilding);
			dismiss();
		}
	};
}
